<?php
namespace app\admin\controller;

use think\Controller;
use app\admin\model\TmModel as TmModels;
use think\Db;

class Tm extends Common
{
    private $cModel;   //当前控制器关联模型

    public function _initialize()
    {
        parent::_initialize();
        $this->cModel = new TmModels;   //别名：避免与控制名冲突
    }

    public function index()
    {
        $where = [];
        if (input('get.search')){
            $where['title|type|contect'] = ['like', '%'.input('get.search').'%'];
        }
        if (input('get._sort')){
            $order = explode(',', input('get._sort'));
            $order = $order[0].' '.$order[1];
        }else{

        }
        $dataList = $this->cModel->join('dy_tv_type a','a.id=dy_tv.fid')->
        field('dy_tv_type.id,type,dy_tv.id,title,pic,contect,dy_tv_type.status,dy_tv.status,dy_tv.create_time,dy_tv.update_time,dy_tv_type.create_time,dy_tv_type.update_time,jid ')->
        where( $where)->order($order)->paginate('3', false, page_param());

        $this->assign('dataList', $dataList);
        return $this->fetch();
    }

    public function create()
    {
        if (request()->isPost()){
            $data = input('post.');
            $result = $this->cModel->validate(CONTROLLER_NAME.'.add')->allowField(true)->save($data);
            if ($result){
                return ajaxReturn(lang('action_success'), url('index'));
            }else{
                return ajaxReturn($this->cModel->getError());
            }
        }else{
            $dataList=Db::table('dy_tv_type')->select();
            $this->assign('dataList', $dataList);
            return $this->fetch('edit');
        }
    }

    public function edit($id)
    {
        if (request()->isPost()){
            $data = input('post.');
            if (count($data) == 2){
                foreach ($data as $k =>$v){
                    $fv = $k!='id' ? $k : '';
                }
                $result = $this->cModel->validate(CONTROLLER_NAME.'.'.$fv)->allowField(true)->save($data, $data['id']);
            }else{
                $result = $this->cModel->validate(CONTROLLER_NAME.'.edit')->allowField(true)->save($data, $data['id']);
            }
            if ($result){
                return ajaxReturn(lang('action_success'), url('index'));
            }else{
                return ajaxReturn($this->cModel->getError());
            }
        }else{
            $dataList=Db::table('dy_tv_type')->select();
            $this->assign('dataList', $dataList);
            $data = $this->cModel->get($id);

            $this->assign('data', $data);
            return $this->fetch();
        }
    }

    public function delete()
    {
        if (request()->isPost()){
            $id = input('id');
            if (isset($id) && !empty($id)){
                $id_arr = explode(',', $id);
                $where = [ 'id' => ['in', $id_arr] ];
                $result = $this->cModel->where($where)->delete();
                if ($result){
                    return ajaxReturn(lang('action_success'), url('index'));
                }else{
                    return ajaxReturn($this->cModel->getError());
                }
            }
        }
    }
}